home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / dyptrk.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  3KB  |  134 lines

  1. /**********************************************************************
  2. *
  3. *    DYPTRK Version 52
  4. *
  5. **********************************************************************
  6. *
  7. *   Dynamic Pitch Tracker
  8. *
  9. *  Inputs:
  10. *   AMDF   - Average Magnitude Difference Function array
  11. *   LTAU   - Number of lags in AMDF
  12. *   MINPTR - Location of minimum AMDF value
  13. *   VOICE  - Voicing decision
  14. *  Outputs:
  15. *   PITCH  - Smoothed pitch value, 2 frames delayed
  16. *   MIDX   - Initial estimate of current frame pitch
  17. *  Compile time constant:
  18. *   DEPTH  - Number of frames to trace back
  19. */
  20.  
  21. #include "lpcdefs.h"
  22.  
  23. extern float s[60];
  24. extern int p[60][2];
  25.  
  26. dyptrk( amdf, minptr, voice, pitch, midx )
  27. float amdf[];
  28. int minptr, voice, *pitch, *midx;
  29. {
  30. float sbar, minsc, maxsc, alpha;
  31. int depth=2;
  32. int pbar, i, j, iptr, path[2];
  33. static float alphax, ipoint=0.0;
  34.  
  35.  
  36. /*   Calculate the confidence factor ALPHA, used as a threshold slope in
  37. *   SEESAW.  If unvoiced, set high slope so that every point in P array
  38. *   is marked as a potential pitch frequency.  A scaled up version (ALPHAX)
  39. *   is used to maintain arithmetic precision.    */
  40.  
  41. if( voice == 1 ) 
  42.     alphax = .75*alphax + amdf[minptr]*.5;
  43. else
  44.     /*alphax = (63./64.)*alphax;*/
  45.     alphax = (0.984375)*alphax;
  46. alpha = alphax*0.06250;
  47.  
  48. if( voice == 0 && alphax < 128 ) alpha = 8;
  49.  
  50. /*  SEESAW: Construct a pitch pointer array and intermediate winner function
  51. *   Left to right pass:        */
  52.  
  53. iptr = ipoint+1;
  54. p[0][iptr-1] = 1;
  55. i = 1;
  56. pbar = 1;
  57. sbar = s[0];
  58.  
  59. for(i=1;i<=LTAU;i++)    {
  60.     sbar += alpha;
  61.     if (sbar < s[i-1]) {
  62.         s[i-1] = sbar;
  63.         p[i-1][iptr-1] = pbar;
  64.     }
  65.     else    {
  66.         sbar = s[i-1];
  67.         p[i-1][iptr-1] = i;
  68.         pbar = i;
  69.     }
  70. }
  71.  
  72. /*   Right to left pass:    */
  73.  
  74. i = pbar-1;
  75. sbar = s[i];
  76. while (i>=1)    {
  77.     sbar += alpha;
  78.     if (sbar< s[i-1]) {
  79.         s[i-1] = sbar;
  80.         p[i-1][iptr-1] = pbar;
  81.     }
  82.     else    {
  83.         pbar = p[i-1][iptr-1];
  84.         i = pbar;
  85.         sbar = s[i-1];
  86.     }
  87.     i--;
  88. }
  89.  
  90. /*   Update S using AMDF
  91. *   Find maximum, minimum, and location of minimum    */
  92.  
  93. s[0] += amdf[1]*0.5;
  94. minsc = s[0];
  95. maxsc = minsc;
  96. *midx = 1;
  97.  
  98. for(i=2;i<=LTAU;i++)    {
  99.     s[i-1] += amdf[i]*0.5;
  100.     if(s[i-1] > maxsc) maxsc = s[i-1];
  101.     if(s[i-1] < minsc) *midx = i;
  102.     if(s[i-1] < minsc) minsc = s[i-1];
  103. }
  104.  
  105. /*   Subtract MINSC from S to prevent overflow    */
  106.  
  107. for(i=1;i<=LTAU;i++)
  108.     s[i-1] -= minsc;
  109. maxsc -= minsc;
  110.  
  111. /*   Use higher octave pitch if significant null there    */
  112.  
  113. j = 0;
  114. for(i=20;i<=40;i+=10)    
  115.     if (*midx > i) 
  116.         if (s[*midx-i-1] < maxsc*0.25) j = i;
  117. *midx -= j;
  118.  
  119. /*   TRACE: look back two frames to find minimum cost pitch estimate    */
  120.  
  121. j = ipoint;
  122. *pitch = *midx;
  123. for(i=1;i<=depth;i++)    {
  124.     j = j%depth+1;
  125.     *pitch = p[*pitch-1][j-1];
  126.     path[i-1] = *pitch;
  127. }
  128. ipoint = ((int)(ipoint)+depth-1)%depth;
  129.  
  130.  
  131.  
  132.  
  133. }
  134.